#include <app/App.h>
#include <router/RouterClient.h>

App::App(int fd):
    fd(fd)
{

}

Endpoint App::get_sender()
{
    return sender;
}

Endpoint App::get_receiver()
{
    return receiver;
}

int App::get_fd()
{
    return fd;
}

void App::send_message_to(message msg, Endpoint receiver)
{
    msg.set_receiver(receiver);
    msg.set_sender(sender);
    router_client.put_message(msg);
}

bool App::send_to_fd(std::span<char> data)
{
    char* buffer = data.data();
    int remains = data.size();
    while (remains != 0)
    {
        int write_n = write(fd, buffer + data.size() - remains, remains);
        if (write_n == -1)
            return false;

        remains -= write_n;
    }
    return true;
}

void App::set_sender(Endpoint sender)
{
    this->sender = sender;
}

void App::set_receiver(Endpoint receiver)
{
    this->receiver = receiver;
}
